package com.lyy.blog.service;

import com.lyy.blog.dao.typerepositroy;
import com.lyy.blog.notfoundeexception;
import com.lyy.blog.po.type;
import javassist.NotFoundException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class typeserviceimpl implements typeservice {

    @Autowired
    private typerepositroy typerepositroy;


    @Transactional
    public type savaType(type type) {
        return typerepositroy.save(type);


    }
    @Transactional
    @Override
    public type getType(Long id) {

        return  typerepositroy.getOne(id);
    }
    @Transactional
    @Override
    public Page<type> listtype(Pageable pageable) {
        return  typerepositroy.findAll(pageable);
    }

    @Override
    public List<type> listtype() {
        return typerepositroy.findAll();
        //return null;
    }

    @Override
    public List<type> listtypetop(Integer size) {

      //  Sort sort=new Sort(Sort.Direction.DESC,"blogs.size");
        Pageable pageable= PageRequest.of(0,size,Sort.Direction.DESC,"blogs.size");
        return typerepositroy.findTop(pageable);
    }

    @Transactional
    @Override
    public type updateType(Long id, type type) {
       type t=typerepositroy.getOne(id);
       if(t==null){
           throw  new notfoundeexception("不存在该类型");



       }
       //////这而不太明白
        BeanUtils.copyProperties(type,t);
       return typerepositroy.save(t);

    }
    @Transactional
    @Override
    public void deleteType(Long id) {

        typerepositroy.deleteById(id);
    }

    @Override
    public type getTypebyname(String name) {

        return typerepositroy.findByName(name);

    }
}
